package com.rojored.module.task
{

import com.rojored.module.api.LoadModuleErrorMsg;
import com.rojored.module.api.LoadModuleMsg;
import com.rojored.module.api.LoadModuleReadyMsg;
import com.rojored.module.api.ModuleContextReadyMsg;
import org.spicefactory.lib.task.Task;
import org.spicefactory.lib.task.enum.TaskState;

/**
 *  Task that will trigger a module load and will complete once the module is
 *  ready.  Works in conjunction with <code>ModuleViewLoaders</code>
 *  configured with a <code>ExplicitLoadPolicy</code> class.
 *
 *  @see com.rojored.module.ExplicitLoadPolicy;
 *  @see http://sourceforge.net/adobe/cairngorm/wiki/HowToUseCairngormModule
 */
public class LoadModuleTask extends Task
{

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    /**
     *  Constructor
     */
    public function LoadModuleTask(moduleId:String = null)
    {
        super();
        this.moduleId = moduleId;
    }

    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

    //--------------------------------------
    //  dispatcher
    //--------------------------------------

    [MessageDispatcher]

    /**
     *  Parsley message dispatcher.
     */
    public var dispatcher:Function;

    //--------------------------------------
    //  moduleId
    //--------------------------------------

    /**
     *  Id of the module to be loaded.
     */
    public var moduleId:String;

    //--------------------------------------
    //  waitForContextReady
    //--------------------------------------

    /**
     *  If <code>true</code> the task will wait for the context to be ready
     *  before completing.  Otherwise, the task will complete on stock flex
     *  module ready.
     */
    public var waitForContextReady:Boolean = true;

    //--------------------------------------------------------------------------
    //
    //  Overriden methods: Task
    //
    //--------------------------------------------------------------------------

    /**
     *  @private
     */
    override protected function doStart():void
    {
        dispatcher(new LoadModuleMsg(moduleId));
    }

    //--------------------------------------------------------------------------
    //
    //  Message handlers.
    //
    //--------------------------------------------------------------------------

    [MesssageHandler]

    /**
     *  @private
     */
    public function loadModuleReadyHandler(msg:LoadModuleReadyMsg):void
    {
        if (state != TaskState.ACTIVE)
            return;

        if (msg.moduleId != moduleId)
            return;

        if (waitForContextReady)
            return;

        complete();
    }

    [MessageHandler]

    /**
     *  @private
     */
    public function moduleContextReadyHandler(msg:ModuleContextReadyMsg):void
    {
        if (state != TaskState.ACTIVE)
            return;

        if (msg.moduleId != moduleId)
            return;

        if (!waitForContextReady)
            return;

        complete();
    }

    [MessageHandler]

    /**
     *  @private
     */
    public function loadModuleErrorHandler(msg:LoadModuleErrorMsg):void
    {
        if (state != TaskState.ACTIVE)
            return;

        if (msg.moduleId != moduleId)
            return;

        error(["Error loading module:", moduleId, ":", msg.detail].join(""));
    }
}
}
